package Ad;

import javax.swing.text.Element;
import javax.swing.text.PasswordView;
import java.io.InputStream;
import java.security.PublicKey;
import java.util.Scanner;
import java.util.jar.JarEntry;

/*
方法版本的ATM
 */
public class DiLiuTian {

    public static void main(String[]args){
        process();

    }
    static String[][]users={
            //账号      密码    余额
            {"aaaaaa","111111","1000"},
            {"bbbbbb","222222","2000"},
            {"cccccc","333333","3000"},
            {"dddddd","444444","4000"},
            {"eeeeee","555555","5000"},
    };
    static int index = 0;
    static Scanner sc = new Scanner(System.in);
    //控制整个流程的方法
    public static void process(){
        System.out.println("欢迎使用lovo银行");
        while (true){
            login();
            menu();
        }

    }
    //登录方法
    public static void login(){
        for (int i = 0;i < 3;i++ ){
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc .next();
            for (int j = 0;j< users.length;j++){
                if (users[j][0].equals(account)&&users[j][1].equals(pwd)){
                    System.out.println("登录成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号或密码错误，请重新输入，还剩"+(2-i)+"次");
        }
        System.out.println("次数已用完，卡已被吞");
        System.exit(0);//系统结束

    }
    //菜单界面
    public static void menu(){
        while (true){
        System.out.println("请选择：1-查询 2-存款 3-取款 4-转账 5-切换账号 6-退出 7-修改密码");
        int input = sc.nextInt();
        if(input == 1){
            search();
        }else if (input==2){
            save();
        }else if (input==3){
            get();
        }else if (input==4){
            transfer();
        }else if (input==5){
            System.out.println("账号已退出");
            return;
        }else if (input==6){
            System.out.println("谢谢使用");
            System.exit(0);
        }else if (input==7){
            change();
        }
        else {
            System.out.println("选择有误，请重新选择");
        }
        }
    }
    //存款
    public static void save(){
        System.out.println("请输入存入的金额：");
        double money = sc.nextDouble();
        users[index][2] = Double.parseDouble(users[index][2])+money+"";
        System.out.println("存款成功,您的余额为："+users[index][2]);
    }
    //取款
    public static void get(){
        System.out.println("请输入取款金额：");
        double money = sc.nextDouble();
        if (money <= Double.parseDouble(users[index][2])){
            users[index][2] = Double.parseDouble(users[index][2])-money+"";
            System.out.println("取款成功，您的余额为："+users[index][2]);
        }else{
            System.out.println("您的余额不足");
        }
    }
    //查询
    public static void search(){
        System.out.println("您的余额为："+users[index][2]);
    }
    //转账
    public static void transfer(){
        System.out.println("请输入转账号码：");
        String sd = sc.next();
        if (sd.equals(users[index][0])){
            System.out.println("不能转账给自己");
        }
        else if (sd == users[index][0]){
                System.out.println("请输入转账金额：");
                double money = sc.nextDouble();
                if (money <= Double.parseDouble(users[index][2])){
                    users[index][2] = Double.parseDouble(users[index][2])-money+"";
                    users[index][2] = Double.parseDouble(users[index][2])+money+"";
                    System.out.println("转账成功，您的余额为："+users[index][2]);
                }else{
                    System.out.println("您的余额不足");
                }
            }
        System.out.println("输入的账号不存在");
    }
    //更改账户密码
    public static void change(){
        Scanner input = new Scanner(System.in);
        boolean b = false;
        while (true){
            System.out.println("请输入密码：");
            String cipher = input.next();
            if (cipher.equals(users[index][1])){
                System.out.println("请输入新密码：");
                String password = input.next();
                System.out.println("请再次输入密码：");
                String password1 = input.next();
                if (password.equals(password1)){
                    System.out.println("密码修改失败，新密码不能和老密码一样");
                }else if (password !=password1){
                    System.out.println("两次密码不一致，请重新输入：");
                    continue;
                }
                String cipher1 = users[index][1];
                users[index][1] = password;
                System.out.println("密码修改成功，请重新登录");
                b = true;
                break;
            }
            if (b){
              login();
            }
        }
    }
}
